package com.kunlun.system.web;


import org.springframework.stereotype.Controller;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.kunlun.system.service.ISysUserService;

import jdk.nashorn.internal.objects.annotations.Function;

import com.kunlun.baseconf.BaseController;
import com.kunlun.baseconf.JsonResult;
import com.kunlun.baseconf.UserBean;
import com.kunlun.system.entity.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author admin
 * @since 2019-04-21
 */
@Controller
public class SysUserController extends BaseController<SysUser>{
    private final Logger logger = LoggerFactory.getLogger(SysUserController.class);

    private static final String MODEL = "/admin/user";
    
    private static final String orderByField = "id";
    
    
    
    @Autowired
    public ISysUserService iSysUserService;

    /**
     * 跳转列表页面
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(method= RequestMethod.GET,value = {"/sysUserIndex"})
    public String index(HttpServletRequest request, Model model) {
        return "/web/system/sysUserListIndex";
       
    }

    @RequestMapping("/list")
    public String  listUser(HttpServletRequest request,SysUser  sysUser,Model model) {   	
    	Page  query= setQueryPage(request,sysUser,orderByField);   	
    	Page<SysUser>  page=iSysUserService.selectPage(query);   	
    	model.addAttribute("page", page);   	
        return "/web/system/sysUserList";
    }
    
    @RequestMapping("/login")
    public String  login(HttpServletRequest request,SysUser  sysUser,Model model) {   	   	
        return "/login";
    }
    
   
    @RequestMapping("/loginCheck")
    public String  loginCheck(HttpServletRequest request,SysUser  sysUser,Model model) {   	   	   
    	Map<String,Object>  columnMap=new HashMap<String,Object>();
    	columnMap.put("code", sysUser.getCode());
    	columnMap.put("password", sysUser.getPassword());	
    	List<SysUser> list = iSysUserService.selectByMap(columnMap);
    	   	
    	 if(list!=null && list.size()>0) {
    		 UserBean  userBean=new UserBean();
    		 userBean.setLoginUser(list.get(0));		 
    		 request.getSession().setAttribute("userBean", userBean);   		 
    		 return "/index";
    	 }   	   	
    	return "/login";
    }
    
    
    
    @RequestMapping("/listData")
    @ResponseBody
    public Page<SysUser>  listUserData(HttpServletRequest request,SysUser  sysUser,Model model) {   	
    	Page  query= setQueryPage(request,sysUser,orderByField);   	
    	Page<SysUser>  page=iSysUserService.selectPage(query);   	       	
        return page;
    } 
    
    
    @GetMapping(MODEL + "/list.json")
	//@Function("user.query")
	@ResponseBody
	public JsonResult<Page<SysUser>> indexJson(HttpServletRequest request,SysUser  sysUser,Model model) {
    	Page<SysUser>  query= setQueryPage(request,sysUser,orderByField); 
		return JsonResult.success(iSysUserService.selectPage(query));
	}
    
    
    
    

    /**
     * 跳转添加页面
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(method=RequestMethod.GET,value="/sysUserAdd")
    public String sysUserAdd(HttpServletRequest request,HttpServletResponse response,Model model) {
        try {


        }catch (Exception ex){
            logger.error("sysUserAdd -=- {}",ex.toString());
        }
        return "sysUserAdd";
    }

    /**
     * 跳转修改页面
     * @param request
     * @param id  实体ID
     * @return
     */
    @RequestMapping(method=RequestMethod.GET,value="/sysUserUpdate")
    public String sysUserUpdate(HttpServletRequest request,Long id) {
        try {
            SysUser sysUser = iSysUserService.selectById(id);
            request.setAttribute("sysUser",sysUser);
        }catch (Exception ex){
            logger.error("sysUserUpdate -=- {}",ex.toString());
        }
        return "sysUserUpd";
    }

    /**
     * 保存和修改公用的
     * @param sysUser  传递的实体
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method=RequestMethod.POST,value="/sysUserSave")
    public int sysUserSave(SysUser sysUser) {
        int count = 0;
        try {
            count = iSysUserService.insertOrUpdate(sysUser) ? 1 : 0;
        } catch (Exception e) {
            logger.error("sysUserSave -=- {}",e.toString());
        }
        return count;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method= RequestMethod.POST,value="/sysUserDelete")
    public int sysUserDelete(Long id){
        int count = 0;
        try {
            count = iSysUserService.deleteById(id) ? 1 : 0;
        }catch (Exception e){
            logger.error("sysUserDelete -=- {}",e.toString());
        }
        return count;
    }

    /**
     * 批量删除对象
     * @param item 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method= RequestMethod.POST,value="/sysUserBatchDelete")
    public int deleteBatchIds(String item){
        int count = 0;
        try {
            List<Long> ids = (List<Long>) JSON.parse(item);
            count = iSysUserService.deleteBatchIds(ids) ? 1 : 0;
        }catch (Exception e){
            logger.error("sysUserBatchDelete -=- {}",e.toString());
        }
        return count;
    }


}